package com.mrjoew.weekend.services.impl.bz;

import com.mrjoew.weekend.entities.ValidException;
import com.mrjoew.weekend.entities.dto.RegisterDTO;
import com.mrjoew.weekend.entities.po.SysAccountPO;
import com.mrjoew.weekend.repositories.sys.SysAccountRepository;
import com.mrjoew.weekend.services.bz.RegisterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @author Christopher Jones
 * @ClassName RegisterServiceImpl
 * @explain
 * @date 2020/11/7 0007 21:14
 */
@Slf4j
@Service
public class RegisterServiceImpl implements RegisterService {

    private final SysAccountRepository sysAccountRepository;

    public RegisterServiceImpl(SysAccountRepository sysAccountRepository) {
        this.sysAccountRepository = sysAccountRepository;
    }

    @Override
    public void register(RegisterDTO in) {
        Optional<SysAccountPO> firstByAccount = sysAccountRepository.findFirstByAccount(in.getAccount());
        if (firstByAccount.isPresent()) {
            throw new ValidException("账号已存在");
        }

        sysAccountRepository.save(new SysAccountPO(in.getAccount(), in.getPassword()));
    }
}
